package com.lonewalker.sparrow.handler;

import com.lonewalker.sparrow.domain.ApiResult;
import com.lonewalker.sparrow.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author lonewalker
 * @desc 异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 其它所有异常
     */
    @ExceptionHandler(Exception.class)
    public ApiResult<String> handleException(Exception exception) {
        log.error(exception.getMessage());
        return ApiResult.errorByMessage("服务器出错啦，请联系管理员");
    }

    /**
     * 处理自定义服务异常
     *
     * @param exception 自定义服务异常
     * @return 提示信息
     */
    @ExceptionHandler(ServiceException.class)
    public ApiResult<String> handleServiceException(ServiceException exception) {
        return ApiResult.errorByMessage(exception.getMessage());
    }
}
